/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package todomanager.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import todomanager.data.ITask;

/**
 * Class for writing the database of todo-items to an XML-file.
 * @author jonasbirgersson, gihoon Kim
 */
public class TodoXMLWriter implements ITodoExporter {

    private BufferedWriter bw;
    private ArrayList mainList;
    //private EditWork editArray;

   
    public TodoXMLWriter(BufferedWriter bw) {
        this.bw = bw;
    }

    /**
     * Standard constructor.
     */
    public TodoXMLWriter() {
        
    }
    
 
    public void exportTodo(ArrayList<ITask> tasks) {
        try {
            Element root = new Element("document");
            ITask currentTask;
            Iterator<ITask> taskIterator = tasks.iterator();
            while (taskIterator.hasNext()) {
                currentTask = taskIterator.next();
                Element task = new Element("task");
                Element taskName = new Element("name");
                taskName.setText(currentTask.getName());
                task.addContent(taskName);
                Element taskDescription = new Element("description");
                taskDescription.setText(currentTask.getDescription());
                task.addContent(taskDescription);
                Element taskDueDate = new Element("dueDate");
                taskDueDate.setText(currentTask.getDueDate());
                task.addContent(taskDueDate);
                Element taskCategory = new Element("category");
                taskCategory.setText(currentTask.getCategory());
                task.addContent(taskCategory);
                Element taskPriority = new Element("priority");
                taskPriority.setText(new Integer(currentTask.getPriority()).toString());
                task.addContent(taskPriority);
                Element taskProgress = new Element("progress");
                taskProgress.setText(new Integer(currentTask.getProgress()).toString());
                task.addContent(taskProgress);
                Element taskDone = new Element("done");
                taskDone.setText(new Boolean(currentTask.isDone()).toString());
                task.addContent(taskDone);
                root.addContent(task);
            }


            Document doc = new Document(root);
            XMLOutputter serializer = new XMLOutputter();
            FileOutputStream outStream = new FileOutputStream("database.xml");
            serializer.output(doc, outStream);
        } catch (IOException ex) {
            //Logger.getLogger(TodoXMLWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void deletetask(int id) throws IOException{
    	try{
    		ITodoImporter read = new TodoXMLReader(new BufferedReader(
					new FileReader("database.xml")));
			mainList = read.importTodo();
//    		int help1 = Integer.parseInt(help);
			System.out.println(id);
    		mainList.remove(id-1);
    		Element root = new Element("document");
            ITask currentTask;
            Iterator<ITask> taskIterator = mainList.iterator();
            while (taskIterator.hasNext()) {
                currentTask = taskIterator.next();
                Element task = new Element("task");
                Element taskName = new Element("name");
                taskName.setText(currentTask.getName());
                task.addContent(taskName);
                Element taskDescription = new Element("description");
                taskDescription.setText(currentTask.getDescription());
                task.addContent(taskDescription);
                Element taskDueDate = new Element("dueDate");
                taskDueDate.setText(currentTask.getDueDate());
                task.addContent(taskDueDate);
                Element taskCategory = new Element("category");
                taskCategory.setText(currentTask.getCategory());
                task.addContent(taskCategory);
                Element taskPriority = new Element("priority");
                taskPriority.setText(new Integer(currentTask.getPriority()).toString());
                task.addContent(taskPriority);
                Element taskProgress = new Element("progress");
                taskProgress.setText(new Integer(currentTask.getProgress()).toString());
                task.addContent(taskProgress);
                Element taskDone = new Element("done");
                taskDone.setText(new Boolean(currentTask.isDone()).toString());
                task.addContent(taskDone);
                root.addContent(task);
            }


            Document doc = new Document(root);
            XMLOutputter serializer = new XMLOutputter();
            FileOutputStream outStream = new FileOutputStream("database.xml");
            serializer.output(doc, outStream);
            
    	}catch (IOException ex) {
            //Logger.getLogger(TodoXMLWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void EditListForWrite(ITask edit,ArrayList help) throws IOException {
        try {
        	File databaseFile = new File("database.xml");
    		if (databaseFile.exists()) {
    			ITodoImporter read = new TodoXMLReader(new BufferedReader(
    					new FileReader("database.xml")));
    			mainList = read.importTodo();
    		}
    		String helpstr = help.get(0).toString();
    		int help1 = Integer.parseInt(helpstr);
    		mainList.remove(help1);
    		mainList.add(help1,edit);
            Element root = new Element("document");
            ITask currentTask;
            Iterator<ITask> taskIterator = mainList.iterator();
            while (taskIterator.hasNext()) {
                currentTask = taskIterator.next();
                Element task = new Element("task");
                Element taskName = new Element("name");
                taskName.setText(currentTask.getName());
                task.addContent(taskName);
                Element taskDescription = new Element("description");
                taskDescription.setText(currentTask.getDescription());
                task.addContent(taskDescription);
                Element taskDueDate = new Element("dueDate");
                taskDueDate.setText(currentTask.getDueDate());
                task.addContent(taskDueDate);
                Element taskCategory = new Element("category");
                taskCategory.setText(currentTask.getCategory());
                task.addContent(taskCategory);
                Element taskPriority = new Element("priority");
                taskPriority.setText(new Integer(currentTask.getPriority()).toString());
                task.addContent(taskPriority);
                Element taskProgress = new Element("progress");
                taskProgress.setText(new Integer(currentTask.getProgress()).toString());
                task.addContent(taskProgress);
                Element taskDone = new Element("done");
                taskDone.setText(new Boolean(currentTask.isDone()).toString());
                task.addContent(taskDone);
                root.addContent(task);
            }


            Document doc = new Document(root);
            XMLOutputter serializer = new XMLOutputter();
            FileOutputStream outStream = new FileOutputStream("database.xml");
            serializer.output(doc, outStream);
        } catch (IOException ex) {
            //Logger.getLogger(TodoXMLWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

}
